package cn.itcast.bos.action;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletOutputStream;

import org.apache.commons.lang3.StringUtils;
import org.apache.jasper.tagplugins.jstl.core.ForEach;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.struts2.ServletActionContext;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import cn.itcast.bos.domain.Region;
import cn.itcast.bos.domain.Subarea;
import cn.itcast.bos.service.subarea.SubareaServiceImpl;
import cn.itcast.bos.utils.FileUtils;
import net.sf.json.JSONArray;
import net.sf.json.JsonConfig;

@Controller
@Scope("prototype")
public class SubareaAction extends BaseAction<Subarea> {

	
	//显示分区信息列表
	public String pageQuery(){
		String addresskey = model.getAddresskey();
		Region region = model.getRegion();
		DetachedCriteria subareaDC = pageBean.getDetachedCriteria();
		
		if (StringUtils.isNotBlank(addresskey)) {
			//按照地址关键字进行模糊查询
			subareaDC.add(Restrictions.like("addresskey", "%"+addresskey+"%"));
		}
		if (region != null) {
			DetachedCriteria regionDC = subareaDC.createCriteria("region");
			
			//得到region里面的 省  市 区
			String province = region.getProvince();
			String city = region.getCity();
			String district = region.getDistrict();
			
			if (StringUtils.isNotBlank(province)) {
				regionDC.add(Restrictions.like("province", "%"+province+"%"));
			}
			if (StringUtils.isNotBlank(city)) {
				regionDC.add(Restrictions.like("city", "%"+city+"%"));
			}
			if (StringUtils.isNotBlank(district)) {
				regionDC.add(Restrictions.like("district", "%"+district+"%"));
			}
		}
		
		subareaService.pageQuery(pageBean);
		this.writePageBeanToJson(pageBean, new String[]{"decidezone","subareas"});
		
		/*List<Subarea> list = subareaService.findAll();
		JsonConfig jsonConfig = new JsonConfig();
		jsonConfig.setExcludes(new String[]{"region"});
		JSONArray jsonArray = JSONArray.fromObject(list,jsonConfig);
		String json = jsonArray.toString();
		
		this.getBackJson(json);*/
		return NONE;
	}
	
	public String save(){
		subareaService.save(model);
		return "list";
	}
	
	//导出分区数据
	public String exportXls() throws IOException{
		//查询所有分区的数据
		List<Subarea> list = subareaService.findAll();
		//将list集合中的数据写到一个Excel表格当中
		HSSFWorkbook workbook = new HSSFWorkbook();
		HSSFSheet sheet = workbook.createSheet("分区数据");
		HSSFRow headRow = sheet.createRow(0);
		headRow.createCell(0).setCellValue("分区编号");
		headRow.createCell(1).setCellValue("区域编码");
		headRow.createCell(2).setCellValue("关键字");
		headRow.createCell(3).setCellValue("起始号");
		headRow.createCell(4).setCellValue("结束号");
		headRow.createCell(5).setCellValue("单双号");
		headRow.createCell(6).setCellValue("位置");
		//循环list
		for (Subarea subarea : list) {
			HSSFRow dataRow = sheet.createRow(sheet.getLastRowNum()+1);
			dataRow.createCell(0).setCellValue(subarea.getId());
			dataRow.createCell(1).setCellValue(subarea.getRegion().getId());
			dataRow.createCell(2).setCellValue(subarea.getAddresskey());
			dataRow.createCell(3).setCellValue(subarea.getStartnum());
			dataRow.createCell(4).setCellValue(subarea.getEndnum());
			dataRow.createCell(5).setCellValue(subarea.getSingle());
			dataRow.createCell(6).setCellValue(subarea.getPosition());
		}
		
		//文件下载  一个输出流  两个头
		ServletOutputStream stream = ServletActionContext.getResponse().getOutputStream();
		String filename = "分区数据.xls";
		filename = FileUtils.encodeDownloadFilename(filename, 
				ServletActionContext.getRequest().getHeader("user-agent"));
		ServletActionContext.getResponse().setContentType(
				ServletActionContext.getServletContext().getMimeType(filename));
		ServletActionContext.getResponse().setHeader("content-disposition", 
				"attachment;filename="+filename);
		workbook.write(stream);
		
		
		return NONE;
	}
	
	//查询分区
	public String findSubareaAjax(){
		DetachedCriteria dc = DetachedCriteria.forClass(Subarea.class);
		dc.add(Restrictions.isNull("decidezone"));
		List<Subarea> list = subareaService.findByCondition(dc);
		this.writeListToJson(list, new String[]{"decidezone","region","endnum","single","startnum"});
		return NONE;
	}
}
